<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Verifies digital signature of x509 certificate against a public key</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.openssl-x509-read.html">« openssl_x509_read</a></li>
      <li style="float: right;"><a href="class.opensslcertificate.html">OpenSSLCertificate »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.openssl.html">OpenSSL 函数</a></li>
    <li>Verifies digital signature of x509 certificate against a public key</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.openssl-x509-verify" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">openssl_x509_verify</h1>
  <p class="verinfo">(PHP 7 &gt;= 7.4.0, PHP 8)</p><p class="refpurpose"><span class="refname">openssl_x509_verify</span> &mdash; <span class="dc-title">Verifies digital signature of x509 certificate against a public key</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.openssl-x509-verify-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>openssl_x509_verify</strong></span>(<span class="methodparam"><span class="type"><span class="type"><a href="class.opensslcertificate.html" class="type OpenSSLCertificate">OpenSSLCertificate</a></span>|<span class="type">string</span></span> <code class="parameter">$certificate</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="class.opensslasymmetrickey.html" class="type OpenSSLAsymmetricKey">OpenSSLAsymmetricKey</a></span>|<span class="type"><a href="class.opensslcertificate.html" class="type OpenSSLCertificate">OpenSSLCertificate</a></span>|<span class="type">array</span>|<span class="type">string</span></span> <code class="parameter">$public_key</code></span>): <span class="type">int</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>openssl_x509_verify()</strong></span> verifies that the 
   <code class="parameter">certificate</code> certificate was signed by the private 
   key corresponding to public key <code class="parameter">public_key</code>.  
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.openssl-x509-verify-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
<dt>
<code class="parameter">x509</code></dt>

<dd>

 <p class="para">
  参见<a href="openssl.certparams.html" class="link">密钥／证书参数</a>以获取有效值列表。
 </p>
</dd>


    
     <dt>
<code class="parameter">public_key</code></dt>

     <dd>

      <p class="para">
       <span class="classname"><a href="class.opensslasymmetrickey.html" class="classname">OpenSSLAsymmetricKey</a></span> - a key, returned by <span class="function"><a href="function.openssl-get-publickey.html" class="function">openssl_get_publickey()</a></span>
      </p>
      <p class="para">
       <span class="type">string</span> - a PEM formatted key, example, &quot;-----BEGIN PUBLIC KEY-----
MIIBCgK...&quot;
      </p>     
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.openssl-x509-verify-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns 1 if the signature is correct, 0 if it is incorrect, and
   -1 on error.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.openssl-x509-verify-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">certificate</code> accepts an <span class="classname"><a href="class.opensslcertificate.html" class="classname">OpenSSLCertificate</a></span> instance now;
       previously, a <a href="language.types.resource.html" class="link">资源(resource)</a> of type <code class="literal">OpenSSL X.509</code> was accepted.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">public_key</code> accepts an <span class="classname"><a href="class.opensslasymmetrickey.html" class="classname">OpenSSLAsymmetricKey</a></span>
       or <span class="classname"><a href="class.opensslcertificate.html" class="classname">OpenSSLCertificate</a></span> instance now;
       previously, a <a href="language.types.resource.html" class="link">资源(resource)</a> of type <code class="literal">OpenSSL key</code> or <code class="literal">OpenSSL X.509</code>
       was accepted.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.openssl-x509-verify-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-964">
    <p><strong>示例 #1 <span class="function"><strong>openssl_x509_verify()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$hostname&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"news.php.net"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$ssloptions&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"capture_peer_cert"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"capture_peer_cert_chain"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"allow_self_signed"</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"CN_match"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$hostname</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"verify_peer"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"SNI_enabled"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"SNI_server_name"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$hostname</span><span style="color: #007700">,<br />);<br />&nbsp;<br /></span><span style="color: #0000BB">$ctx&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_context_create</span><span style="color: #007700">(&nbsp;array(</span><span style="color: #DD0000">"ssl"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$ssloptions</span><span style="color: #007700">)&nbsp;);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_socket_client</span><span style="color: #007700">(</span><span style="color: #DD0000">"ssl://</span><span style="color: #0000BB">$hostname</span><span style="color: #DD0000">:443"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errno</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errstr</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">30</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STREAM_CLIENT_CONNECT</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$ctx</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$cont&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_context_get_params</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$x509&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$cont</span><span style="color: #007700">[</span><span style="color: #DD0000">"options"</span><span style="color: #007700">][</span><span style="color: #DD0000">"ssl"</span><span style="color: #007700">][</span><span style="color: #DD0000">"peer_certificate"</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$certparsed&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">openssl_x509_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$x509</span><span style="color: #007700">);<br /><br />foreach(</span><span style="color: #0000BB">$cont</span><span style="color: #007700">[</span><span style="color: #DD0000">"options"</span><span style="color: #007700">][</span><span style="color: #DD0000">"ssl"</span><span style="color: #007700">][</span><span style="color: #DD0000">"peer_certificate_chain"</span><span style="color: #007700">]&nbsp;as&nbsp;</span><span style="color: #0000BB">$chaincert</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$chainparsed&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">openssl_x509_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$chaincert</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$chain_public_key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">openssl_get_publickey</span><span style="color: #007700">(</span><span style="color: #0000BB">$chaincert</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$r&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">openssl_x509_verify</span><span style="color: #007700">(</span><span style="color: #0000BB">$x509</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$chain_public_key</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$r</span><span style="color: #007700">==</span><span style="color: #0000BB">1</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$certparsed</span><span style="color: #007700">[</span><span style="color: #DD0000">'subject'</span><span style="color: #007700">][</span><span style="color: #DD0000">'CN'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;was&nbsp;digitally&nbsp;signed&nbsp;by&nbsp;"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$chainparsed</span><span style="color: #007700">[</span><span style="color: #DD0000">'subject'</span><span style="color: #007700">][</span><span style="color: #DD0000">'CN'</span><span style="color: #007700">].</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>

  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.openssl-x509-verify-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.openssl-verify.html" class="function" rel="rdfs-seeAlso">openssl_verify()</a> - 验证签名</span></li>
    <li class="member"><span class="function"><a href="function.openssl-get-publickey.html" class="function" rel="rdfs-seeAlso">openssl_get_publickey()</a> - 别名 openssl_pkey_get_public</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>